home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Tool Chest / Development Tools & Languages / Dylan Related / Marlais / Marlais 0.5.9-portable sources / bytestring.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-15  |  5.2 KB  |  204 lines  |  [TEXT/ttxt]

  1. /*
  2.  
  3.    bytestring.c
  4.  
  5.    This software is free software; you can redistribute it and/or
  6.    modify it under the terms of the GNU Library General Public
  7.    License as published by the Free Software Foundation; either
  8.    version 2 of the License, or (at your option) any later version.
  9.  
  10.    This software is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.    Library General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU Library General Public
  16.    License along with this software; if not, write to the Free
  17.    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.    Original copyright notice follows:
  20.  
  21.    Copyright, 1993, Brent Benson.  All Rights Reserved.
  22.    0.4 & 0.5 Revisions Copyright 1994, Joseph N. Wilson.  All Rights Reserved.
  23.  
  24.    Permission to use, copy, and modify this software and its
  25.    documentation is hereby granted only under the following terms and
  26.    conditions.  Both the above copyright notice and this permission
  27.    notice must appear in all copies of the software, derivative works
  28.    or modified version, and both notices must appear in supporting
  29.    documentation.  Users of this software agree to the terms and
  30.    conditions set forth in this notice.
  31.  
  32.  */
  33.  
  34. #include <string.h>
  35.  
  36. #include "bytestring.h"
  37.  
  38. #include "alloc.h"
  39. #include "character.h"
  40. #include "collection.h"
  41. #include "error.h"
  42. #include "number.h"
  43. #include "prim.h"
  44. #include "symbol.h"
  45.  
  46. /* primitives */
  47.  
  48. static Object string_element (Object string, Object index, Object default_ob);
  49. static Object string_element_setter (Object string, Object index, Object val);
  50. static Object string_size (Object string);
  51. static Object string_append2 (Object str1, Object str2);
  52. static Object string_lessthan (Object str1, Object str2);
  53. static Object string_equal (Object str1, Object str2);
  54.  
  55. static struct primitive string_prims[] =
  56. {
  57.     {"%string-element", prim_3, string_element},
  58.     {"%string-element-setter", prim_3, string_element_setter},
  59.     {"%string-size", prim_1, string_size},
  60.     {"%string-append2", prim_2, string_append2},
  61.     {"%string<", prim_2, string_lessthan},
  62.     {"%string=", prim_2, string_equal},
  63. };
  64.  
  65. /* function definitions */
  66.  
  67. void
  68. init_string_prims (void)
  69. {
  70.     int num;
  71.  
  72.     num = sizeof (string_prims) / sizeof (struct primitive);
  73.  
  74.     init_prims (num, string_prims);
  75.  
  76. }
  77.  
  78. Object
  79. make_byte_string (char *str)
  80. {
  81.     Object obj;
  82.  
  83.     obj = allocate_object (sizeof (struct byte_string));
  84.  
  85.     BYTESTRTYPE (obj) = ByteString;
  86.     BYTESTRSIZE (obj) = strlen (str);
  87.     BYTESTRVAL (obj) = checking_strdup (str);
  88.     return (obj);
  89. }
  90.  
  91. Object
  92. make_string_driver (Object args)
  93. {
  94.     int size, i;
  95.     char fill;
  96.     Object size_obj, fill_obj, res;
  97.  
  98.     size = 0;
  99.     size_obj = NULL;
  100.     fill_obj = NULL;
  101.     while (!NULLP (args)) {
  102.     if (FIRST (args) == size_keyword) {
  103.         size_obj = SECOND (args);
  104.     } else if (FIRST (args) == fill_keyword) {
  105.         fill_obj = SECOND (args);
  106.     } else {
  107.         error ("make: unsupported keyword for <string> class", FIRST (args), NULL);
  108.     }
  109.     args = CDR (CDR (args));
  110.     }
  111.     if (size_obj) {
  112.     if (!INTEGERP (size_obj)) {
  113.         error ("make: value of size: argument must be an integer", size_obj, NULL);
  114.     }
  115.     size = INTVAL (size_obj);
  116.     }
  117.     if (fill_obj) {
  118.     if (!CHARP (fill_obj)) {
  119.         error ("make: value of fill: must be a character for <string> class", fill_obj, NULL);
  120.     }
  121.     fill = CHARVAL (fill_obj);
  122.     } else {
  123.     fill = 'a';
  124.     }
  125.  
  126.     /* actually fabricate the string */
  127.     res = allocate_object (sizeof (struct byte_string));
  128.  
  129.     BYTESTRTYPE (res) = ByteString;
  130.     BYTESTRSIZE (res) = size;
  131.     BYTESTRVAL (res) = (char *) checking_malloc ((size * sizeof (char)) + 1);
  132.  
  133.     for (i = 0; i < size; ++i) {
  134.     BYTESTRVAL (res)[i] = fill;
  135.     }
  136.     BYTESTRVAL (res)[i] = '\0';
  137.     return (res);
  138. }
  139.  
  140. /* primitives */
  141.  
  142. static Object
  143. string_element (Object string, Object index, Object default_ob)
  144. {
  145.     int i;
  146.  
  147.     i = INTVAL (index);
  148.     if ((i < 0) || (i >= BYTESTRSIZE (string))) {
  149.     if (default_ob == default_object) {
  150.         error ("element: argument out of range", string, index, NULL);
  151.     } else {
  152.         return default_ob;
  153.     }
  154.     }
  155.     return (make_character (BYTESTRVAL (string)[i]));
  156. }
  157.  
  158. static Object
  159. string_element_setter (Object string, Object index, Object val)
  160. {
  161.     int i;
  162.  
  163.     i = INTVAL (index);
  164.     if ((i < 0) || (i >= BYTESTRSIZE (string))) {
  165.     error ("element-setter: argument out of range", string, index, NULL);
  166.     }
  167.     BYTESTRVAL (string)[i] = CHARVAL (val);
  168.     return (unspecified_object);
  169. }
  170.  
  171. static Object
  172. string_size (Object string)
  173. {
  174.     return (make_integer (BYTESTRSIZE (string)));
  175. }
  176.  
  177. static Object
  178. string_append2 (Object str1, Object str2)
  179. {
  180.     char *new_str;
  181.     int new_size;
  182.  
  183.     new_size = BYTESTRSIZE (str1) + BYTESTRSIZE (str2);
  184.     new_str = (char *) checking_malloc ((new_size * sizeof (char)) + 1);
  185.  
  186.     strcpy (new_str, BYTESTRVAL (str1));
  187.     strcat (new_str, BYTESTRVAL (str2));
  188.     return (make_byte_string (new_str));
  189. }
  190.  
  191. static Object
  192. string_lessthan (Object str1, Object str2)
  193. {
  194.     return (strcmp (BYTESTRVAL (str1), BYTESTRVAL (str2)) < 0) ?
  195.     true_object : false_object;
  196. }
  197.  
  198. static Object
  199. string_equal (Object str1, Object str2)
  200. {
  201.     return (strcmp (BYTESTRVAL (str1), BYTESTRVAL (str2)) == 0) ?
  202.     true_object : false_object;
  203. }
  204.